package com.ykdl.member.kid.adapters;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import android.R.integer;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.BitmapUtils;
import com.ykdl.member.constant.KidAction;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.beans.OperationOrderBean;
import com.ykdl.member.kid.beans.OrderBean;
import com.ykdl.member.kid.marketcard.CouponinfoWebView;
import com.ykdl.member.kid.marketcard.PaySelectActivity;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;

/********************************************************************************
 * 
 ** 作者： jiafeiyu 创始时间：2015-5-5 描述： 我的订单之adapter
 *********************************************************************************/
public class MyOrderAdapter extends BaseAdapter {

	private List<OrderBean> data_list;
	private BitmapUtils bitmapUtil;
	private Context mContext;
	private LayoutInflater inflater;
	private RelativeLayout my_rl_bottom;
	private Map<String, Integer> statusmap = new HashMap<String, Integer>();
	private boolean onclick=true;
	public MyOrderAdapter(Context context) {
		mContext = context;
		bitmapUtil = new BitmapUtils(context);
		bitmapUtil.configDefaultLoadingImage(R.drawable.defult_head_icon);
		bitmapUtil.configDefaultLoadFailedImage(R.drawable.defult_head_icon);
		inflater = LayoutInflater.from(context);

	}
	public  void clearList() {
		statusmap.clear();
	}
	public void setData(List<OrderBean> data_list) {
		this.data_list = data_list;
	}

	@Override
	public int getCount() {
		if (data_list != null && data_list.size() > 0) {
			return data_list.size();
		} else {
			return 0;
		}

	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		if (getCount() != 0) {
			return data_list.get(position-1);
		} else {
			return null;
		}
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.item_myorder_adapter, null);
			holder.rl_bottom = (RelativeLayout) convertView
					.findViewById(R.id.rl_bottom);
			// my_rl_bottom = holder.rl_bottom;
			holder.iv_myorder_pic_back = (ImageView) convertView
					.findViewById(R.id.iv_myorder_pic_back);
			holder.tv_my_order_time = (TextView) convertView
					.findViewById(R.id.tv_my_order_time);
			holder.rl_myorder_click = (RelativeLayout) convertView
					.findViewById(R.id.rl_myorder_click);
			holder.tv_myorder_product_name = (TextView) convertView
					.findViewById(R.id.tv_myorder_product_name);
			holder.tv_myorder_product_price = (TextView) convertView
					.findViewById(R.id.tv_myorder_product_price);
			holder.tv_my_order_total_price = (TextView) convertView
					.findViewById(R.id.tv_my_order_total_price);
			// holder.tv_myorder_product_number = (TextView)
			// convertView.findViewById(R.id.tv_myorder_product_number);
			holder.tv_my_order_status = (TextView) convertView
					.findViewById(R.id.tv_my_order_status);
			holder.bt_current_pay = (Button) convertView
					.findViewById(R.id.bt_current_pay);
			holder.bt_cancel_order = (Button) convertView
					.findViewById(R.id.bt_cancel_order);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		initView(holder, position);
		return convertView;
	}

	private void initView(ViewHolder holder, int position) {
		
		OrderBean bean = data_list.get(position);
		holder.tv_my_order_time.setText(bean.getCreated_time()); // "order_status":
		String url = "";
		if (null != bean.getOrder_product_list().get(0).getProduct_image()) {
			url = bean.getOrder_product_list().get(0).getProduct_image();
		}
		bitmapUtil.display(holder.iv_myorder_pic_back, url);
		holder.tv_myorder_product_name.setText(bean.getOrder_product_list()
				.get(0).getProduct_name());
		holder.tv_myorder_product_price.setText("￥"
				+ bean.getOrder_product_list().get(0).getProduct_price() + "x"
				+ bean.getOrder_product_list().get(0).getProduct_count());
		// holder.tv_myorder_product_number.setText("共" + bean.getCount()
		// +"件商品");
		holder.tv_my_order_total_price.setText("￥" + bean.getOrder_price());
		holder.rl_myorder_click.setTag(bean);
		holder.tv_my_order_status.setText(bean.getOrder_status_desc());
		holder.rl_myorder_click.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				OrderBean bean = (OrderBean) v.getTag();
				Intent intent = new Intent(mContext, CouponinfoWebView.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.putExtra(KidAction.ORDER_ID, bean.getOrder_id()+"");
				intent.putExtra(KidAction.URL_TYPE, 2);
				mContext.startActivity(intent);
			}
		});

		if (!onclick && statusmap.containsKey(bean.getOrder_id() + "")) {
			holder.rl_bottom.setVisibility(View.GONE);
			if (statusmap.get(bean.getOrder_id() + "") == 23111) {
				holder.tv_my_order_status.setText("待发货");
			} else if (statusmap.get(bean.getOrder_id() + "") == 23132) {
				holder.tv_my_order_status.setText("已完成");
			} else if (statusmap.get(bean.getOrder_id() + "") == 23212) {
				holder.tv_my_order_status.setText("退款中");
			}else {
				holder.tv_my_order_status.setText("已关闭");
			}
		} else {
			holder.rl_bottom.setVisibility(View.VISIBLE);
			switch (bean.getOrder_status()) {
			case 11111:
				holder.bt_cancel_order.setVisibility(View.VISIBLE);
				holder.bt_current_pay.setVisibility(View.GONE);
				break;
			case 12111:
				holder.bt_cancel_order.setVisibility(View.VISIBLE);
				holder.bt_current_pay.setVisibility(View.GONE);
				break;
			case 23111:
				holder.bt_cancel_order.setVisibility(View.VISIBLE);
				holder.bt_current_pay.setVisibility(View.VISIBLE);
				break;
			case 23112:
				holder.bt_cancel_order.setVisibility(View.VISIBLE);
				holder.bt_current_pay.setVisibility(View.GONE);
				break;
			case 23122:
				holder.bt_cancel_order.setVisibility(View.VISIBLE);
				holder.bt_current_pay.setVisibility(View.VISIBLE);
				holder.bt_cancel_order.setText(R.string.view_logistics);// 查看物流
				holder.bt_cancel_order.setTag(R.string.view_logistics);
				holder.bt_current_pay.setText(R.string.confirm_receipt);// 确认收货
				holder.bt_current_pay.setTag(R.string.confirm_receipt);
				break;
			default:
				holder.rl_bottom.setVisibility(View.GONE);
				holder.bt_cancel_order.setVisibility(View.GONE);
				holder.bt_current_pay.setVisibility(View.GONE);
				break;
			}
		}

		holder.bt_current_pay.setTag(bean);
		holder.bt_current_pay.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				onclick = false;
				OrderBean bean = (OrderBean) v.getTag();
				if (bean.getOrder_status()==23122) {
					statusmap.put(bean.getOrder_id()+"", 23132);
					OperationOrder(bean.getOrder_id()+"",true,false);
					MyOrderAdapter.this.notifyDataSetChanged();
//					Toast.makeText(mContext, "确认收货", 0).show();
				} else {
					Intent intent = new Intent(mContext, PaySelectActivity.class);
					intent.putExtra(KidAction.ORDER_ID, bean.getOrder_id()+"");
					intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					mContext.startActivity(intent);
				}
			}
		});
		holder.bt_cancel_order.setTag(bean);
		holder.bt_cancel_order.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				onclick = false;
				OrderBean bean = (OrderBean) v.getTag();
				if (bean.getOrder_status()==23122) {
					Intent intent = new Intent(mContext, CouponinfoWebView.class);
					intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					intent.putExtra(KidAction.ORDER_ID, bean.getOrder_id()+"");
					intent.putExtra(KidAction.URL_TYPE, 9);
					mContext.startActivity(intent);
				} else if(bean.getOrder_status()==23112){
					getWhetherCancleOrderDialog(mContext,bean.getOrder_id()+"");
					
				}else {
					isCancleOrderDialog(mContext,bean.getOrder_id()+"");
					

				}
			}
		});

	}

	class ViewHolder {
		RelativeLayout rl_myorder_click, rl_bottom;

		TextView tv_my_order_time;

		ImageView iv_myorder_pic_back;
		TextView tv_my_order_status;
		TextView tv_myorder_product_name;
		TextView tv_myorder_product_price;
		// 总价格
		TextView tv_my_order_total_price;
		// 产品数量
		TextView tv_myorder_product_number;
		Button bt_current_pay;
		Button bt_cancel_order;
	}
	public void OperationOrder(String order_id,boolean isOk,boolean isShow) {
		String url="";
		if (isOk) {
			url = KidConfig.ORDER_CONFIRM_GOODS;
		}else {
			url = KidConfig.ORDER_CANCEL;
		}
		Map<String, String> map = new HashMap<String, String>();
		map.put("order_id", order_id);
		HttpBaseRequest request = Wxxr.getInstance().getRequest(url, map, null);
		TaskManager.startHttpRequest(request, new OperationOrderTag(order_id,isOk,isShow),
				OperationOrderBean.class);
	}

	class OperationOrderTag implements ITag {
		private String order_id;
		private boolean isOk ;
		private boolean isShow ;
		public OperationOrderTag(String order_id,boolean isOk,boolean isShow) {
			// TODO Auto-generated constructor stub
			this.order_id=order_id;
			this.isOk=isOk;
			this.isShow=isShow;
		}

		@Override
		public void refreshUI(Object t, DataState ds) {
			if (t instanceof OperationOrderBean) {
				OperationOrderBean bean = (OperationOrderBean) t;
				if (bean.isResult()) {
						statusmap.put(order_id, bean.getComb_order_status());
						if (!isOk&&isShow) {
							getCancleOrderDialog(mContext);
						}
						
				}else {
					statusmap.remove(order_id);
					MyOrderAdapter.this.notifyDataSetChanged();
					Toast.makeText(mContext, "操作订单失败", 0).show();
				}
			}

		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			return false;
		}

		@Override
		public void refreshCancel() {
		}

	}
	/**
	 * 是否取消订单
	 * 
	 * @param context
	 */
	public  void isCancleOrderDialog(Context context,final String order_id) {
		Dialog dialog = new AlertDialog.Builder(context)
				.setMessage("您是否要取消订单?")
				.setPositiveButton("是", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						statusmap.put(order_id, 0);
						OperationOrder(order_id,false,false);
						MyOrderAdapter.this.notifyDataSetChanged();
					}
				})
				.setNegativeButton("否", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				}).create();
		dialog.show();
	}
	/**
	 * 是否取消订单
	 * 
	 * @param context
	 */
	public  void getWhetherCancleOrderDialog(Context context,final String order_id) {
		Dialog dialog = new AlertDialog.Builder(context)
				.setMessage("您是否要取消订单，取消后订单款项会在7个工作日内原途经返回到您的账户上。")
				.setPositiveButton("是", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						statusmap.put(order_id, 23212);
						OperationOrder(order_id,false,true);
						MyOrderAdapter.this.notifyDataSetChanged();
					}
				})
				.setNegativeButton("否", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				}).create();
		dialog.show();
	}
	/**
	 * 确认取消订单
	 * 
	 * @param context
	 */
	public void getCancleOrderDialog(Context context) {
		Dialog dialog = new AlertDialog.Builder(context)
				.setMessage(
						"您的订单已取消，订单款项会在7个工作日内原途经返回到您的账户，请注意查收，如有问题请及时与我们的客服人员联系！")
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				}).create();
		dialog.show();
	}
}
